package org.example.resourceplatform.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDateTime;

@Entity
@Table(name = "document",
        indexes = {
                @Index(name = "idx_document_audit_status", columnList = "audit_status"),
                @Index(name = "idx_document_upload_time", columnList = "upload_time"),
                @Index(name = "idx_document_user_audit", columnList = "user_id, audit_status")
        })
@Getter
@Setter
@NoArgsConstructor
public class Document {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "doc_id")
    private Integer docId;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "subject_id", nullable = false)
    private Subject subject;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "type_id", nullable = false)
    private DocType docType;

    @Column(name = "doc_name", nullable = false, length = 100)
    private String docName;

    @Column(name = "doc_desc", length = 200)
    private String docDesc;

    @Column(name = "file_path", nullable = false, length = 255)
    private String filePath;

    @Column(name = "file_size", nullable = false)
    private Integer fileSize;

    @Column(name = "audit_status", nullable = false, length = 20)
    private String auditStatus = "pending";

    @Column(name = "reject_reason", length = 200)
    private String rejectReason;

    @CreationTimestamp
    @Column(name = "upload_time", nullable = false, updatable = false)
    private LocalDateTime uploadTime;

    @Column(name = "watermark", length = 100)
    private String watermark;

    @Column(name = "avg_rating", nullable = false)
    private Double avgRating = 0.0;

    @Column(name = "rating_count", nullable = false)
    private Integer ratingCount = 0;

    @Column(name = "download_count", nullable = false)
    private Integer downloadCount = 0;

    public Document(String docName, User user, Subject subject, DocType docType, String filePath, Integer fileSize) {
        this.docName = docName;
        this.user = user;
        this.subject = subject;
        this.docType = docType;
        this.filePath = filePath;
        this.fileSize = fileSize;
    }

    @Override
    public String toString() {
        return "Document{" +
                "docId=" + docId +
                ", docName='" + docName + '\'' +
                ", auditStatus='" + auditStatus + '\'' +
                ", uploadTime=" + uploadTime +
                '}';
    }
}